const mongoose = require('mongoose');

const appointmentSchema = new mongoose.Schema({
    // 客户信息
    customerName: {
        type: String,
        required: true,
        trim: true
    },
    customerPhone: {
        type: String,
        required: true,
        trim: true
    },
    customerEmail: {
        type: String,
        trim: true
    },

    // 服务信息
    serviceId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Service',
        required: true
    },
    serviceName: {
        type: String,
        required: true
    },
    servicePrice: {
        type: Number,
        required: true
    },
    serviceDuration: {
        type: Number,
        required: true,
        comment: '服务时长（分钟）'
    },

    // 技师信息
    technicianId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Technician',
        required: true
    },
    technicianName: {
        type: String,
        required: true
    },

    // 预约时间
    appointmentDate: {
        type: Date,
        required: true
    },
    appointmentTime: {
        type: String,
        required: true,
        comment: '预约时间段，如：09:00-11:00'
    },

    // 状态管理
    status: {
        type: String,
        enum: ['待确认', '已确认', '服务中', '已完成', '已取消'],
        default: '待确认'
    },

    // 支付信息
    paymentStatus: {
        type: String,
        enum: ['未支付', '已支付', '已退款'],
        default: '未支付'
    },
    paymentMethod: {
        type: String,
        enum: ['现金', '微信', '支付宝', '银行卡'],
        default: '微信'
    },

    // 备注信息
    notes: {
        type: String,
        trim: true
    },

    // 时间戳
    createTime: {
        type: Date,
        default: Date.now
    },
    updateTime: {
        type: Date,
        default: Date.now
    },

    // 取消信息
    cancelReason: {
        type: String,
        trim: true
    },
    cancelTime: {
        type: Date
    }
});

// 更新时间中间件
appointmentSchema.pre('save', function (next) {
    this.updateTime = new Date();
    next();
});

// 索引优化
appointmentSchema.index({ customerPhone: 1 });
appointmentSchema.index({ appointmentDate: 1 });
appointmentSchema.index({ technicianId: 1 });
appointmentSchema.index({ status: 1 });

module.exports = mongoose.model('Appointment', appointmentSchema); 